<template>
    <div class="content">
        <!--树形控件-->
        <div style="width:20%;float:left;">
            <el-input placeholder="输入关键字进行过滤" v-model="filterText" style="margin: 8px;width: 200px !important;">
            </el-input>
            <el-tree class="filter-tree" style="margin-left: 20px" :data="categoryList" :props="{children:'sonClassily',label:'proClassilyName'}"
                        :filter-node-method="filterNode" ref="tree">
            </el-tree>
        </div>
        <div style="float: left;width:80%">
            <el-table ref="multipleTable" :data="allProductList">
                <el-table-column label="图片">
						<template slot-scope="s">
							<el-image :src="s.row.proImgSrc" style="width:40px;height:40px;"></el-image>
						</template>
					</el-table-column>
					<el-table-column prop="product.productName" label="名称"></el-table-column>
					<el-table-column prop="abilities" label="规格属性">
						<template slot-scope="s">
							<span v-for="item in s.row.abilities" :key="item.abilityId">{{item.abilityName}}</span>
						</template>
					</el-table-column>
					<el-table-column prop="product.productBrand" label="品牌"></el-table-column>
					<el-table-column label="单价">
						<template slot-scope="s">
							<span>{{s.row.proPrice}}元</span>
						</template>
					</el-table-column>
                    <el-table-column label="报价">
                        <template slot-scope="scope">
                            <el-input v-model="scope.row.offer" placeholder="1.00"></el-input>
                        </template>
                    </el-table-column>
					<el-table-column label="数量">
                        <template slot-scope="scope">
                            <el-input type="number" :max="scope.row.wareNum" v-model="scope.row.productNum" placeholder="1"></el-input>
                        </template>
                    </el-table-column>
					<el-table-column type="selection" label="操作">
					</el-table-column>
            </el-table>
        </div>
        <el-button type="primary" plain style="margin:0 auto;display:block;" @click="sure_add">确定</el-button>
    </div>
</template>

<script>
export default {
    data(){
        return{
            // 关键字过滤
            filterText: '',
            allProductList:[],
            categoryList:[]
        }
    },
    methods:{
        // 关键字过滤函数
        filterNode(value, data) {
            if (!value) return true;
            return data.proClassilyName.indexOf(value) !== -1;
        },
        // 产品列表查询
        all_product(){
            this.axios.get("api/product/product-list",{params:{"currPage":1,"pageSize":9999}}).then(v=>{
                this.allProductList=v.data.data;
            }).catch()
        },
        // 树形控件，产品种类查询
        classily(){
            this.axios.get("api/product/classily-list").then(v=>{
                    this.categoryList=v.data;
                }).catch()
        },
        //分页页码改变事件
        change_page(currPage){
            this.all_product(currPage);
        },
        // 确认按钮点击事件：将选中的商品添加到合同商品列表中
        sure_add(){
            let select_data = this.$refs.multipleTable.selection
            let obj = {}
            window.console.log(obj)
            select_data.forEach(item => {
                if(item.offer <= item.proPrice*0.6){
                    this.global.mes_error(item.product.productName+"报价不能低于原价")
                }else if(item.productNum > item.wareNum){
                    this.global.mes_error(item.product.productName+"库存不足"+item.productNum)
                } else{
                    let flag = true // 是否添加的标识，为true则执行添加
                    // 判断是否已经存在改商品，存在则直接修改数量
                    this.$parent.$parent.addOppoForm.oppoProList.forEach(pro => {
                        // 根据商品id进行判断
                        if (pro.productDetail.proDetailId == item.proDetailId){
                            if (item.productNum){
                                pro.productNum = pro.productNum + parseInt(item.productNum)
                            }
                            else{pro.productNum +=  1}
                            if(item.offer){
                                pro.offer = item.offer
                            }else{
                                pro.offer = item.proPrice
                            }
                            flag = false
                            return
                        }
                    })
                    // 列表中不存在该商品，执行添加
                    if(flag){
                        if (item.productNum){obj.productNum = item.productNum;delete item.productNum}
                        else{obj.productNum = 1}
                        if (item.offer){obj.offer = item.offer;delete item.productNum}
                        else{obj.offer = item.proPrice}
                        obj.productDetail = item
                        this.$parent.$parent.addOppoForm.oppoProList.push(obj)
                    }
                    obj = {}
                }
            })
            // 清除勾选
            this.$refs.multipleTable.clearSelection()
            this.$parent.$parent.addProductDialogVisible = false
        }   
    },
    watch: {
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    created(){
        this.all_product();
        this.classily();
    }
}
</script>

<style>

</style>